//
//  TodoViewController.m
//  todo
//
//  Created by Brandon Trebitowski on 9/6/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "TodoViewController.h"
#define BARBUTTON(TITLE, SELECTOR) 	[[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]


@implementation TodoViewController

@synthesize todoText,todoBeginDT,todoEndDT,todo,todoSummary;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
																		 style:UIBarButtonItemStyleBordered 
																		target:self 
																		action:@selector(done:)]; 
		self.navigationItem.leftBarButtonItem = cancelButton; 
		[cancelButton release]; 
		
		
		
	}
	return self;
}

/*
- (void) viewDidLoad {

	
	//by default the begin date is the date of creation, the end date is after 24 hours
	NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
	formatter.dateFormat = @"MM/dd/YY h:mm a";
	NSDate *today =  [[NSDate alloc] init];
	NSString *timestamp = [formatter stringFromDate:today];
	todoBeginDT.text = timestamp;
	[self.todo updateBeginDate:today];
	NSTimeInterval secondsPerDay = 24 * 60 * 60;
	today = [today addTimeInterval:secondsPerDay];
	timestamp = [formatter stringFromDate:today];
	todoEndDT.text = timestamp;
	[self.todo updateEndDate:today];
}
*/


-(void)viewWillAppear:(BOOL)animated {
    NSLog(@"update");
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	UIDatePicker *datePicker = (UIDatePicker *)[actionSheet viewWithTag:101];
	NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
	formatter.dateFormat = @"MM/dd/YY h:mm a";
	
	
	NSString *timestamp = [formatter stringFromDate:datePicker.date];
	//[(UILabel *)[self.view viewWithTag:103] setText:timestamp];
	NSLog(@"%d", [datePicker.date timeIntervalSince1970]);
	NSLog(@"change txt called");
	if (setTextField == 1){
		todoBeginDT.text = timestamp;
		//[self.todo updateBeginDate:datePicker.date];
	} else if (setTextField == 2){
		todoEndDT.text = timestamp;
		//[self.todo updateEndDate:datePicker.date];
	}
	setTextField = 0;
	[actionSheet release];
}


-(void)viewWillDisappear:(BOOL)animated{
	NSLog(@"disappear");
	NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
	formatter.dateFormat = @"MM/dd/yy h:mm a";
	
	[self.todo updateBeginDate:[formatter dateFromString:todoBeginDT.text]];
	[self.todo updateEndDate:[formatter dateFromString:todoEndDT.text]];
	
}
- (IBAction) action: (id) sender
{
	NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n" ;
	
	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Set", nil];
	
	[actionSheet showInView:self.parentViewController.tabBarController.view];
	
	NSLog(@"settextfield = %d, %d", setTextField, [sender tag]);
	UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
	datePicker.tag = 101;
	datePicker.datePickerMode =   UIDatePickerModeDateAndTime;
	
	NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
	formatter.dateFormat = @"MM/dd/yy h:mm a";

	
	if ([sender tag] == 130){
		setTextField = 1;
		NSDate *beginDate = [NSDate date];
		beginDate = [formatter dateFromString:todoBeginDT.text];
		datePicker.date = beginDate;
	} else if ([sender tag] == 131){
		setTextField = 2;
		NSDate *endDate = [NSDate date];
		endDate = [formatter dateFromString:todoEndDT.text];
		datePicker.date = endDate;
	} else {
		setTextField = 0;
	}
	
	[actionSheet addSubview:datePicker];
	
}

- (BOOL)correctDate{
	NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
	formatter.dateFormat = @"MM/dd/yy h:mm a";
	NSDate *beginDate = [NSDate date];
	NSDate *endDate = [NSDate date];
	beginDate = [formatter dateFromString:todoBeginDT.text];
	endDate = [formatter dateFromString:todoEndDT.text];

	if ([beginDate compare: endDate] == NSOrderedDescending){
	
		return FALSE;	
	}

	return TRUE;
}
- (void)done:(id)sender {
	if (![self correctDate]){
		NSLog(@"Not correct date");
		UIAlertView *alert = [[UIAlertView alloc] 
							  initWithTitle:@"Error" 
							  message:@"The end date you have entered occurs before the begin date. Please choose another date." 
							  delegate:self
							  cancelButtonTitle:@"Ok" 
							  otherButtonTitles:nil]; 
		[alert show];
		
		[alert release];
		
	} else {
		
		[self.navigationController setToolbarHidden:NO];
		[[self navigationController] popViewControllerAnimated:YES];
	}
		
} //returnAction

/*

- (IBAction) updateStatus:(id) sender {
	if(self.todo.status == 0) {
		[self.todoButton setTitle:@"Mark As In Progress" forState:UIControlStateNormal];
		[self.todoButton setTitle:@"Mark As In Progress" forState:UIControlStateHighlighted];
		[self.todoStatus setText:@"Complete"];
		[self.todo updateStatus:1];
	} else {
		[self.todoButton setTitle:@"Mark As Complete" forState:UIControlStateNormal];
		[self.todoButton setTitle:@"Mark As Complete" forState:UIControlStateHighlighted];
		[self.todoStatus setText:@"In Progress"];
		[self.todo updateStatus:0];
	}
}

- (IBAction) updatePriority:(id)sender {
	int priority = [self.todoPriority selectedSegmentIndex];
	[self.todo updatePriority:(2-priority+1)];
}
*/

-(IBAction)updateText:(id)sender {
	self.todo.text = self.todoText.text;
	[self.todo updateText];
}

-(IBAction)updateSummary:(id)sender {
	self.todo.summary = self.todoSummary.text;
	[self.todo updateSummary];
}
/*
 Implement loadView if you want to create a view hierarchy programmatically
- (void)loadView {
}
 */

/*
 If you need to do additional setup after loading the view, override viewDidLoad.
- (void)viewDidLoad {
}
 */

// allow only legal input (less than 50 chars, numbers/letters)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
	NSInteger insertDelta = string.length - range.length;
	
	if (textField.text.length + insertDelta > 50)
	{
		return NO; // the new string would be longer than MAX_LENGTH
	}
	return YES;
} //textField

//Hides the keyboard after finishing text input (clicking done)
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	if (textField == todoText) {
		[todoText resignFirstResponder];
	}
	return YES;
} //textFieldShouldReturn


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
}


- (void)dealloc {
	[super dealloc];
}

@end



